package com.ayoka.parenting.ui.group.index;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.CircleGroupAdapter;
import com.ayoka.parenting.bean.CircleGroupBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.databinding.FragmentBaseRecyclerviewBinding;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewFragment;
import com.ayoka.parenting.ui.group.detail.GroupDetailActivity;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GroupIndexFragment.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GroupIndexFragment extends MVPBaseRecyclerViewFragment<CircleGroupBean, FragmentBaseRecyclerviewBinding, GroupIndexContract.View, GroupIndexPresenter> implements GroupIndexContract.View {

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_group_index, null, false);
        adapter.addHeaderView(headerView);
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @Override
    protected BaseQuickAdapter<CircleGroupBean, BaseViewHolder> getAdapter() {
        CircleGroupAdapter adapter = new CircleGroupAdapter();
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CircleGroupBean circleGroupBean = (CircleGroupBean) adapter.getItem(position);
                if (view.getId() == R.id.btn_join_group) {
                    if (mPresenter != null) {
                        mPresenter.joinGroup(circleGroupBean.ID, position);
                    }
                } else if (view.getId() == R.id.btn_enter_group) {
                    enterGroup(circleGroupBean);
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CircleGroupBean circleGroupBean = (CircleGroupBean) adapter.getItem(position);
                enterGroup(circleGroupBean);
            }
        });
        return adapter;
    }

    private void enterGroup(CircleGroupBean circleGroupBean) {
        GroupDetailActivity.start(getActivity(), circleGroupBean.ID, circleGroupBean.FounderUid,  circleGroupBean.Title);
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        if (mPresenter != null) {
            mPresenter.getCircleGroupListData(isShowWaitDialog, pageNo, pageSize);
        }
    }

    @Override
    public void initData() {
        getData(true);
    }

    @Override
    protected GroupIndexPresenter getPresenter() {
        return new GroupIndexPresenter();
    }

    @Override
    public void configCircleGroupListData(PagerBean<CircleGroupBean> data) {
        if (data == null) return;
        setLoadMoreLayout(data.list, data.totalPage);
    }

    @Override
    public void onOperationCallback(boolean isSuccess, String groupId) {
        if (isSuccess) {
            ((CircleGroupAdapter) adapter).notifyCircleGroupChanged(groupId);
        }
    }
}
